package ProjectTeam39.Networking;
import ProjectTeam39.Domains.*;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;

public class Presenter {
	private Client client;
//	private String host;
//	private int port;
//	private Model backend;
//	private String filename = "doctorsOffice.data";

	public Presenter(String hostName, int port){
//		host = hostName;
//		this.port = port;
		this.client = new Client(hostName, port);
	    startClient();
//		this.backend = this.load(filename);
	}
	
/*	public int login(String username, String password){
		int userlvl = 0;
		return userlvl;
	}
	
	public boolean addPatient(Patient p){
		
	}
	
	public Patient getPatient(String username){
		client.send(new Message(Message.MessageType.REQUESTUSER, username));
	}
	
	public boolean updatePatient(Patient updatedp){
		
	}
	public Doctor getDoctor(String username){
		
	}
*/
	public UserList getUserList(){
		Message m = new UserListMessage(null, Message.MessageType.REQUEST);
		Message result = null;
		UserList ul = null;	
		client.send(m);	
		while(result==null){
			result = client.receive();
		}
		ul = (UserList) result.getContents();
		return ul;
	}
	
	public void setUserList(UserList ul){
		Message m = new UserListMessage(ul, Message.MessageType.UPDATE);
		client.send(m);
	}
	
	public AppointmentCalender getAppointmentCalender(){
		Message m = new AppointmentCalenderMessage(null, Message.MessageType.REQUEST);
		Message result = null;
		AppointmentCalender ac = null;	
		client.send(m);	
		while(result==null){
			result = client.receive();
		}
		ac = (AppointmentCalender) result.getContents();
		return ac;
	}
	
	public void setAppointmentCalender(AppointmentCalender apptcal){
		
	}
	
	public Model getModel(){
		Message m = new ModelMessage(null);
		Message result = null;
		Model model = null;	
		client.send(m);	
		while(result==null){
			result = client.receive();
		}
		model = (Model) result.getContents();
		return model;
	}
	
	public void save(String filename){
	      try {
	    	  Model myModel = getModel();
	          ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream(filename));
	          out.writeObject(myModel);
	       } catch (FileNotFoundException e) {
	          System.err.println("Failed to open file " + filename + " for output");
	          e.printStackTrace();
	       } catch (IOException e) {
	          System.err.println("Failed during save for unknown reason");
	          e.printStackTrace();
	       }
	}
	
	public Model load(String filename){
		Model model = null;
		try {
	         ObjectInputStream in = new ObjectInputStream(new FileInputStream(filename));     
	         model = (Model) in.readObject();
	         setAppointmentCalender(model.getAppointmentCalender());
	         setUserList(model.getUserList());
	      } catch (FileNotFoundException e) {
	         System.err.println("Could not find file " + filename + " for opening");
	         e.printStackTrace();
	      } catch (IOException e) {
	         System.err.println("Other input failure during object read");
	         e.printStackTrace();
	      } catch (ClassNotFoundException e) {
	         System.err.println("Failed to find class type on object read");
	         e.printStackTrace();
	      }
	      return model;

	}
	
	public void startClient(){
		this.client.connect();
		this.client.getStreams();
	}
	
	public void closeClient(){
		this.client.shutdown();
	}
}
